﻿using System;
using System.Text.RegularExpressions;

namespace SDT.CrawlSystem.Servers.Filters
{
    /// <summary>
    /// Written by TungNX
    /// </summary>
    public sealed class WIDFilter : IWIDFilter
    {
        #region Constructors
        public WIDFilter(int startAt, string startSign, string endSign, string regEx)
        {
            StartAt = startAt;
            StartSign = startSign;
            EndSign = endSign;
            RegEx = regEx;
        }
        #endregion

        #region Fields Members
        private int startAt = 0;
        public int StartAt
        {
            get { return startAt; }
            set { startAt = value; }
        }

        private string startSign = "";
        public string StartSign
        {
            get { return startSign; }
            set { startSign = value != null ? value.Trim() : ""; }
        }

        private string endSign = "";
        public string EndSign
        {
            get { return endSign; }
            set { endSign = value != null ? value.Trim() : ""; }
        }

        private string regEx = "";
        public string RegEx
        {
            get { return regEx; }
            set { regEx = value != null ? value.Trim() : ""; }
        }
        #endregion

        private static char[] unwanted = new char[] { '.', '_', '-', '/', '&', '=', ' ' };
        /// <summary>
        /// Modified Date : 14/09/2010
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public string GetWID(string url)
        {
            #region Get WID by signs
            if (!string.IsNullOrEmpty(startSign))
            {
                var s = url.IndexOf(startSign, startAt);
                if (s >= 0)
                {
                    var e = endSign.Length > 0 ? url.IndexOf(endSign, s + startSign.Length) : url.Length;
                    if (e > s)
                    {
                        try 
                        {
                            //Modified Date : 14/09/2010
                            //Remove unwanted
                            //return Convert.ToString(url.Substring(s + startSign.Length, e - s - startSign.Length).Trim(unwanted));
                            return Convert.ToString(url.Substring(s + startSign.Length, e - s - startSign.Length).Trim());
                        }
                        catch { }
                    }
                }
            }
            #endregion
            #region Get WID by regular expression
            else if (!string.IsNullOrEmpty(RegEx))
            {
                var col = Regex.Matches(url, RegEx);
                foreach (Match m in col)
                {
                    if (m.Success)
                    {
                        try { return Convert.ToString(m.Value.Trim(unwanted)); }
                        catch { }
                    }
                }
            }
            #endregion

            return string.Empty;
        }
    }
}